<?php

namespace pay\modules\frontend\models;

use pay\libs\Tools;
use Yii;

/**
 * This is the model class for table "pay_order_details".
 *
 * @property int $id
 * @property int $oid 订单id
 * @property string $channel_id 支付渠道
 * @property string $openid 用户id
 * @property string $goods_name 商品名称
 * @property string $goods_desc 商品描述
 * @property int $pay_money 支付金额
 * @property string $is_refund 1-已退款,2-未退款
 * @property string $client_id 客户端ip
 * @property string $facility 客户端设备
 * @property int $refund_money 退款金额
 * @property string $refund_time 退款时间
 * @property string $from 来源
 * @property string $pay_times 支付时间戳，毫秒
 * @property string $callback_url 回调地址
 * @property string $remake 备注
 */
class OrderDetails extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'pay_order_details';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['oid'], 'required'],
            [['oid'], 'integer'],
            [['is_refund'], 'string'],
            [['refund_time','channel_id','pay_money','refund_money'], 'safe'],
            [['goods_name', 'goods_desc', 'callback_url'], 'string', 'max' => 255],
            [['client_id','openid'], 'string', 'max' => 32],
            [['facility', 'from'], 'string', 'max' => 64],
            [['pay_times'], 'string', 'max' => 15],
            [['remake'], 'string', 'max' => 1024],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'oid' => 'Oid',
            'goods_name' => 'Goods Name',
            'goods_desc' => 'Goods Desc',
            'pay_money' => 'Pay Money',
            'is_refund' => 'Is Refund',
            'channel_id' => 'Channel Id',
            'client_id' => 'Client ID',
            'facility' => 'Facility',
            'refund_money' => 'Refund Money',
            'refund_time' => 'Refund Time',
            'from' => 'From',
            'pay_times' => 'Pay Times',
            'callback_url' => 'Callback Url',
            'remake' => 'Remake',
        ];
    }


    public function addInfo($oid,$data)
    {
        $data['OrderDetails']['oid'] = $oid;
        $data['OrderDetails']['is_refund'] = '2';
        $data['OrderDetails']['client_id'] = Yii::$app->request->userIP;
        $data['OrderDetails']['goods_name'] = $data['commodityName'];
        $data['OrderDetails']['goods_desc'] = $data['goodsDesc'];
        $data['OrderDetails']['callback_url'] = $data['callBackUrl'];
        $data['OrderDetails']['pay_money'] = $data['amount'];
        $data['OrderDetails']['client_id'] = Yii::$app->request->userIP;
        $data['OrderDetails']['from'] = isset($data['from'])?$data['from']:'';
        $data['OrderDetails']['remake'] =  isset($data['remark'])?$data['remark']:$data['attach'];
        $data['OrderDetails']['channel_id'] =  $data['channelMerId'];
        $data['OrderDetails']['openid'] =  isset($data['userId'])?$data['userId']:'';
        if ($data['amount'] == 0){
            $data['OrderDetails']['pay_times'] = (string)Tools::get_msectime();
        }
        if ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }
        return false;
    }


    /*** 退款添加信息
     * @param $oid
     * @param $data
     * @return bool
     */
    public function addRefundInfo($oid,$data)
    {
        $model = self::find()->where('oid = :oid',[':oid' => $oid])->one();
        if (!is_null($model)){
            $model->is_refund = '1';
            $model->refund_money = $data['refundFee'];
            $model->refund_time = date("Y-m-d H:i:s");
            if ($model->save(0)){
                return true;
            }
        }
        return false;
    }


    public function queryOrderInfoList($params)
    {
        $page = 1;
        $pageSize = 10;
        $channel_id = '';
        $openid = '';
        extract($params);
        $model = self::find();
        $model->where(['or',['!=','pay_times',''],['pay_money' => 0]]);
        $model->andFilterWhere(['channel_id' => $channel_id]);
        $model->andFilterWhere(['openid' => $openid]);
        $model->andFilterWhere(['from' => '新龙生态林门票']);
        $count = $model->count();
        $list = $model->offset(($page - 1)*$pageSize)->orderBy(['id' => SORT_DESC])->limit($pageSize)->asArray()->all();
        return [
            'list' => $list,
            'page' => $page,
            'pageSize' => $pageSize,
            'count' => $count,
        ];
    }
}
